package com.zhongkai.web.control.config;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.config.Role;
import com.zhongkai.service.config.RoleService;
import com.zhongkai.tools.CheckWord;

@Controller
public class RoleControl {
	
	private Logger log=Logger.getLogger(this.getClass());

	private RoleService roleService;

	@Resource
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	@RequestMapping(value="/system/config/role_add.do")
	public String save(Role roleForm,HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out=response.getWriter();
		try {
			String roleName=roleForm.getRoleName();
			if(roleName==null || roleName.trim().equals("")){
				out.write(CheckWord.showMsg("请填写角色名称！", "/system/config/role_add_form.jsp",request));
				return null;
			}
			String roleNote=roleForm.getRoleNote();
			if(roleNote!=null && roleNote.length()>100){
				out.write(CheckWord.showMsg("角色描术内容长度不能超过100，请检查！", "/system/config/role_add_form.jsp",request));
				return null;
			}
			Role role=new Role();
			role.setRoleId(null);
			role.setRoleName(roleName);
			role.setRoleNote(roleNote);
			roleService.save(role);
			return "redirect:/system/config/role_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error("错误！添加角色时发生异常："+e.getMessage());
			out.write(CheckWord.showMsg("添加角色时发生异常，请与管理员联系!", "/system/config/role_add_form.jsp",request));
			return null;
		}
	}
	
	@RequestMapping(value="/system/config/role_manage_modify.do")
	public String update(Role roleForm,HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out=response.getWriter();
		try {
			Integer id=roleForm.getRoleId();
			if(id==null || id.intValue()==0){
				out.write(CheckWord.showMsg("非法的角色信息！", "/system/desktop.jsp",request));
				return null;
			}
			String roleName=roleForm.getRoleName();
			if(roleName==null || roleName.trim().equals("")){
				out.write(CheckWord.showMsg("请填写角色名称！", "/system/config/role_manage_modify.jsp?id="+id,request));
				return null;
			}
			String roleNote=roleForm.getRoleNote();
			if(roleNote!=null && roleNote.length()>100){
				out.write(CheckWord.showMsg("角色描术内容长度不能超过100，请检查！", "/system/config/role_manage_modify.jsp?id="+id,request));
				return null;
			}
			Role role=roleService.findById(id);
			if(role==null){
				out.write(CheckWord.showMsg("找不到该角色信息！", "/system/config/role_manage_list.jsp",request));
				return null;
			}
			role.setRoleName(roleName);
			role.setRoleNote(roleNote);
			roleService.update(role);
			return "redirect:/system/config/role_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error("错误！修改角色时发生异常："+e.getMessage());
			out.write(CheckWord.showMsg("修改角色时发生异常，请与管理员联系!", "/system/config/role_manage_list.jsp",request));
			return null;
		}
	}
	
	@RequestMapping(value="/system/config/role_manage_delete.do")
	public String delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out=response.getWriter();
		try {
			String id=request.getParameter("id");
			if(id==null || !id.matches("^[0-9]*[1-9][0-9]*$")){
				out.write(CheckWord.showMsg("非法参数！", "/system/desktop.jsp",request));
				return null;
			}
			Integer roleId=Integer.valueOf(id);
			Role role=roleService.findById(roleId);
			if(role==null){
				out.write(CheckWord.showMsg("找不到该角色信息！", "/system/config/role_manage_list.jsp",request));
				return null;
			}
			Integer userCount=roleService.userCount(roleId);
			if(userCount>0){
				out.write(CheckWord.showMsg("该角色下包含用户，该角色暂时无法删除！", "/system/config/role_manage_list.jsp",request));
				return null;
			}
			roleService.delete(role);
			out.write(CheckWord.showMsg("角色删除成功！", "/system/config/role_manage_list.jsp",request));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("错误！删除角色时发生异常："+e.getMessage());
			out.write(CheckWord.showMsg("删除角色时发生异常，请与管理员联系!", "/system/config/role_manage_list.jsp",request));
		}
		return null;
	}
}
